package com.sgb.kjwl.viewmodel.manage;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.manage.CompanyTradesListEntity;
import com.swgk.core.base.model.entity.ListResponse;
import com.sgb.kjwl.view.ui.fragment.manage.ShopCompanyTrendsFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;

import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 *企业动态
 */
public class ShopCompanyTrendsModel extends BaseViewModel {

    ShopCompanyTrendsFragment shopCompanyTrendsFragment;

    public ShopCompanyTrendsModel(ShopCompanyTrendsFragment shopCompanyTrendsFragment) {
        this.shopCompanyTrendsFragment = shopCompanyTrendsFragment;
    }

    public void getDataForCompanyTrends(Map<String,Object> map){
        DialogHelper.showProgressDialog(shopCompanyTrendsFragment.getContext(), null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getManageAPI().getCompanyTrends(map).enqueue(new Callback<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>> call, Response<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getData() == null) {
                    MToast.showToast(shopCompanyTrendsFragment.getContext(), "暂无数据");
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    shopCompanyTrendsFragment.getDataForCompanyTrends(response.body().getData().getList());
                } else {
                    MToast.showToast(shopCompanyTrendsFragment.getContext(), response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ListResponse<List<CompanyTradesListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
}